﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace FangChanCrm.Controllers
{
    public class PhoneShouYeController : Controller
    {
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_RenYuanQuanXianService t_RenYuanQuanXianService;
        private readonly It_XinWenBiaoService t_XinWenBiaoService;

        public PhoneShouYeController(It_RenYuanBiaoService renYuanBiaoService, It_RenYuanQuanXianService renYuanQuanXianService, It_XinWenBiaoService xinWenBiaoService)
        {
            t_RenYuanBiaoService = renYuanBiaoService;
            t_RenYuanQuanXianService = renYuanQuanXianService;
            t_XinWenBiaoService = xinWenBiaoService;
        }

        public IActionResult XinWenXiangXi()
        {
            return View();
        }

        public IActionResult KaiFaZhong()
        {
            return View();
        }

        public IActionResult XinWenList()
        {
            return View();
        }

        public IActionResult ShouYe()
        {
            var code = Request.Query["code"];

            string access_token = WeiXin_Common.GetAccess_Token();
            string json = WeiXin_Common.GetUserInfo(code, access_token);
            string WeiXinUserId = WeiXin_Common.GetJsonValue(json, "UserId");
            IList<t_RenYuanBiao> list = t_RenYuanBiaoService.GetList().Where(x => x.BeiZhu1 == WeiXinUserId).ToList();
            if (Convert.ToInt32(HttpContext.Session.GetString("loginid")) > 0)
            {
                HttpContext.Session.SetString("UserId", HttpContext.Session.GetString("loginid"));
                var retlistbefore = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Convert.ToInt32(HttpContext.Session.GetString("loginid"))).ToList();
                string[] ret = { "0" };
                if (retlistbefore.Count > 0)
                {
                    ret = retlistbefore[0].QuanXianId.Split(",");
                }
                //var retlist = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Convert.ToInt32(HttpContext.Session.GetString("loginid"))).ToList()[0].QuanXianId.Split(",");
                ViewBag.list = ret;
                return View();
            }
            if (WeiXinUserId == "")
            {
                return View("../RenYuan/BindWeiXin");
            }
            if (list.Count > 0)
            {
                HttpContext.Session.SetString("UserId", list[0].Id.ToString());
                HttpContext.Session.SetString("loginid", list[0].Id.ToString());
                var retlistbefore = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Convert.ToInt32(HttpContext.Session.GetString("loginid"))).ToList();
                string[] ret = { "0" };
                if (retlistbefore.Count > 0)
                {
                    ret = retlistbefore[0].QuanXianId.Split(",");
                }
                //var retlist = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Convert.ToInt32(HttpContext.Session.GetString("loginid"))).ToList()[0].QuanXianId.Split(",");
                ViewBag.list = ret;
                return View();
            }
            else
            {
                ViewData["WeiXinUserId"] = WeiXinUserId;
                return View("../RenYuan/BindWeiXin");
            }
        }

        #region 新闻列表
        public string GetXinWenList()
        {
            try
            {
                string pin = "";

                List<t_XinWenBiao> list = t_XinWenBiaoService.GetXinWenList();
                for (int i = 0; i < list.Count; i++)
                {
                    string neirong = "";
                    if (list[i].XinWenNeiRong.Length > 59)
                    {
                        neirong = list[i].XinWenNeiRong.Substring(0, 59) + "...";
                    }
                    else
                    {
                        neirong = list[i].XinWenNeiRong;
                    }
                    pin += "<ul class='community-list' id='xiaoqubangding' style='color:#808080; width: 100%; height: auto; margin: 0; padding: 5px 10px; list-style: none;' ><li  onclick='tiao(" + list[i].Id + ")' class='list-item'  style='border-radius: 15px; width: 100%; height: auto; background-color: rgb(241, 241, 241); padding: 0 0.5rem; box-shadow: 0 3px 5px 0px #888888; border: 1px solid #888888'><div class='item-top' style='text-align: center;height: 2rem; padding-top: 8px;font-size: 14px'><span>" + list[i].BeiZhu1 + "</span></div><div class='item-top' style='width: 100%; height: auto; padding: 0px 0px; text-indent:40px; font-size:14px'>" + neirong + "</div><div class='item-bottom' style='text-align: right; font-size: 13px; '><span style='color:#FFA500; '><b>发布人：</b></span><span id='sa_shijian'>" + t_RenYuanBiaoService.GetById(list[i].FangBuRenId).XingMing + "</span></div><div class='item-bottom' style='text-align: right; font-size: 13px; '><span style='color:#FFA500; '><b>发布时间：</b></span><span id='sa_shijian'>" + list[i].FaBuShiJian + "</span></div></li></ul>";
                }
                return pin;
            }
            catch (Exception)
            {
                return "";
                //throw;
            }
        }
        #endregion

    }
}
